package com.oceansoft.mobile.biz.api.controller;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.oceansoft.mobile.biz.api.entity.News;
import com.oceansoft.mobile.biz.api.service.IApiNewsService;
import com.oceansoft.mobile.common.Constant;
import com.oceansoft.mobile.common.base.entity.Result;
import com.oceansoft.mobile.common.util.CacheBaseService;
import com.oceansoft.mobile.common.util.HttpClientUtil;
import com.oceansoft.mobile.weixin.util.DateUtil;

@Controller
@RequestMapping("api/news")
public class ApiNewsController {
	
	//页面跳转的里的接口地址均为APP后台接口 APP后期改造需要
	
	@Resource
	private IApiNewsService newsService;

	@ResponseBody
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public Result addNews(News news, HttpServletRequest request)
			throws ParseException {
		String key = "87efbfd9-95e5-4b5c-b7c1-10af1cbe96aa";

		String postKey = request.getParameter("key");
		String pubtime = request.getParameter("pubtime");

		if (StringUtils.isEmpty(postKey) || !key.equals(postKey)) {
			return new Result(Result.FAILURE, "密钥错误:[" + postKey + "]");
		}

		if (StringUtils.isNotEmpty(pubtime)) {
			news.setPubTime(DateUtil.getFormatDate(pubtime));
		} else {
			news.setPubTime(new Date());
		}

		news.setStatus(News.NEWS_UNSEND);
		return newsService.addNews(news);
	}

	/**
	 * 加载新闻咨询
	 * 
	 * @param model
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "index/{channelId}", method = GET)
	public String gotoNewsList(@PathVariable(value="channelId")int channelId,HttpServletRequest request,ModelMap model) throws Exception {
		model.addAttribute("channelId",channelId);
		String channelName = "";
		switch (channelId) {
		case 1:
			channelName = "警方提示";
			break;
		case 2:
			channelName = "安全知识";
			break;
		case 3:
			channelName = "公示公告";
			break;
		case 4:
			channelName = "警务报道";
			break;
		case 5:
			channelName = "防范宣传";
			break;
		case 6:
			channelName = "警务公开";
			break;
		default:
			break;
		}
		
		model.addAttribute("channelName",channelName);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return "weixin/news/new_list";

	}
	
	/**
	 * 加载新闻详情
	 * @param newsId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "viewNews/{newsId}")
	public ModelAndView viewNews(@PathVariable("newsId") long newsId,
			Model model,HttpServletRequest request) {
		model.addAttribute("newId", newsId);
		String source = (String) request.getSession().getAttribute("source");
	       if (StringUtils.isNotBlank(source)) {
	       	model.addAttribute("source", source);
	       }
		return new ModelAndView("weixin/news/new_detail");
	}
	
	@ResponseBody 
    @RequestMapping(value = "list/{channelId}/{pageNo}", method = GET)
    public com.oceansoft.mobile.common.util.Result newsList(@PathVariable("channelId")String channelId,@PathVariable("pageNo")String pageNo) throws IOException {
		String url = Constant.host+"/api/news/list/"+channelId+"/"+pageNo;
		com.oceansoft.mobile.common.util.Result r = null;
        if(false&&CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, com.oceansoft.mobile.common.util.Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
        System.out.println(JSONObject.toJSON(r));
		return r;
    }
	@ResponseBody 
    @RequestMapping(value = "newsDetail/{newsId}", method = GET)
    public com.oceansoft.mobile.common.util.Result newsDetail(@PathVariable("newsId")String newsId) throws IOException {
		String url = Constant.host+"/api/newsDetail/"+newsId;
		com.oceansoft.mobile.common.util.Result r = null;
        if(CacheBaseService.getInstance().exists(url))
        {
        	 r = CacheBaseService.getInstance().getObject(url, com.oceansoft.mobile.common.util.Result.class);
        }
        else
        { 
        	r = HttpClientUtil.doGAGet(url);
        	CacheBaseService.getInstance().cache(url, r, 1f);
        }
		return r;
    }
}
